<?php
App::uses('AppController', 'Controller');
/**
 * Pias Controller
 *
 * @property Pia $Pia
 * @property PaginatorComponent $Paginator
 */
class PiasController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Pia->recursive = 0;
		$this->set('pias', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Pia->exists($id)) {
			throw new NotFoundException(__('Invalid pia'));
		}
		$options = array('conditions' => array('Pia.' . $this->Pia->primaryKey => $id));
		$this->set('pia', $this->Pia->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Pia->create();			
			if ($this->Pia->saveAll($this->request->data['Pias'])) {
				$this->Session->setFlash(__('Datos grabados correctamente.'));
			} else {
				$this->Session->setFlash(__('Ocurrio un error, no se grabaron los datos.'));
			}
		}
		$metas = $this->Pia->Meta->find('list');
		$fuentes = $this->Pia->Fuente->find('list');
		$planContables = $this->Pia->PlanContable->find('all',array(
			'fields' => array('id','codigo','name'),
			'order' => array('codigo')
			));
		$clasificadors = $this->Pia->Clasificador->find('all',array(
			'fields' => array('id','codigo','name'),
			'order' => array('codigo')
			));
		$this->set(compact('metas', 'fuentes'));
		$this->set(array(
			'clasificadors' => $clasificadors,
			'planContables' => $planContables));

	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Pia->exists($id)) {
			throw new NotFoundException(__('Invalid pia'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Pia->save($this->request->data)) {
				$this->Session->setFlash(__('The pia has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The pia could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Pia.' . $this->Pia->primaryKey => $id));
			$this->request->data = $this->Pia->find('first', $options);
		}
		$metas = $this->Pia->Metum->find('list');
		$fuentes = $this->Pia->Fuente->find('list');
		$planContables = $this->Pia->PlanContable->find('list');
		$clasificadors = $this->Pia->Clasificador->find('list');
		$this->set(compact('metas', 'fuentes', 'planContables', 'clasificadors'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Pia->id = $id;
		if (!$this->Pia->exists()) {
			throw new NotFoundException(__('Invalid pia'));
		}
		$this->request->allowMethod('post', 'delete');
		if ($this->Pia->delete()) {
			$this->Session->setFlash(__('The pia has been deleted.'));
		} else {
			$this->Session->setFlash(__('The pia could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
}
